package org.wms.minbearwcs.dao.mission.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import org.wms.minbearwcs.dao.mission.inf.WcsMissionDao;
import org.wms.minbearwcs.entity.Mapper.mission.WcsMissionMapper;
import org.wms.minbearwcs.entity.PO.wcs.mission.WcsMission;
import org.wms.minbearwcs.entity.PO.wcs.mission.status.MissionStatus;
import org.wms.minbearwcs.entity.PO.wcs.mission.type.MissionType;

import java.time.LocalDateTime;
import java.util.List;

@Repository
@RequiredArgsConstructor
public class WcsMissionDaoImpl implements WcsMissionDao {

    private final WcsMissionMapper mapper;

    @Override
    public boolean insert(WcsMission mission) {
        return mapper.insert(mission) == 1;
    }

    @Override
    public List<WcsMission> selectAll() {
        return new LambdaQueryChainWrapper<>(mapper)
                .list();
    }

    @Override
    public MissionStatus selectMissionStatusById(Integer id) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(WcsMission::getId,id)
                .select(WcsMission::getMissionType,WcsMission::getMissionStatus)
                .one()
                .getEnumMissionStatus();

    }

    @Override
    public boolean updateMissionStatusById(Integer id, String status) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(WcsMission::getId,id)
                .set(WcsMission::getMissionStatus,status)
                .update();
    }

    @Override
    public boolean deleteMissionById(Integer id) {
        return mapper.deleteById(id) == 1;
    }

    @Override
    public boolean updateStartTime(Integer id, LocalDateTime now) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(WcsMission::getId,id)
                .set(WcsMission::getStartTime,now)
                .update();
    }
}
